<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/7/4
 * Time: 14:27
 */

namespace app\admin\controller;


use think\Controller;
use think\Db;

class Timer extends Controller
{
    //权限
    protected static $auth_type = [
        2 => '铜牌召集令',
        3 => '银牌召集令',
        4 => '金牌召集令'
    ];
    //推送列表
    protected static $lists = [];
    
    /**
     * 权限到期推送
     */
    public function overduePush()
    {
        $time = strtotime(date('Y-m-d'));
        $res = $this->selectTime();
        $n_title = '召集令已过期通知';
        $n_type = 9;
        foreach($res as $key => $val) {
            if(strtotime($val['uauth_end']) == $time) {//今日刚好过期
                $va = (string)$val['uauth_user_id'];
                array_push(self::$lists, $va);
                $n_content = '您购买' . self::$auth_type[$val['uauth_type']] . '已过期';
                $result = $this->createNotice($n_title, $n_content, $n_type, $val['uauth_user_id']);
                if($result){
                    $this->Jpush(self::$lists, $n_title, 1, '');
                }
            }
        }
    }
    
    /**
     * 提前3天推送，提示即将过期
     */
    public function willOverduePush()
    {
        $time = strtotime(date('Y-m-d', strtotime("+3 days")));
        $res = $this->selectTime();
        $n_title = '召集令即将过期';
        $n_type = 8;
        foreach($res as $key => $val) {
            if(strtotime($val['uauth_end']) == $time) {//3天后过期
                $va = (string)$val['uauth_user_id'];
                array_push(self::$lists, $va);
                $n_content = self::$auth_type[$val['uauth_type']] . '权限即将到期，快来续费吧';
                $result = $this->createNotice($n_title, $n_content, $n_type, $val['uauth_user_id']);
                if($result){
                    $this->Jpush(self::$lists, $n_title, 1, '');
                }
            }
        }
    }
    
    /**
     * @param $n_title  标题
     * @param $n_content  内容
     * @param $n_type   消息类型
     * @param $user_id  推送的用户
     *
     */
    public function createNotice($n_title, $n_content, $n_type, $user_id){
        $data['n_title'] = $n_title;
        $data['n_content'] = $n_content;
        $data['n_createtime'] = date('Y-m-d H:i:s');
        $data['n_type'] = $n_type;
        $data['n_send_type'] = 2;
        $data['n_user_id'] = $user_id;
        $data['n_read_users'] = ',0,';
        $data['n_clear_users'] = ',0,';
        $res = Db::name('notice')->insert($data);
        return $res;
    }
    
    /**
     * 查询用户权限过期时间
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function selectTime()
    {
        $res = Db::name('user_auth')
            ->field('uauth_id, uauth_user_id, uauth_type, uauth_end')
            ->select();
        return $res;
    }
    
    /**
     * @param $client
     * @param $sence
     * @param $orderId
     * @return array|bool
     * 极光推送
     */
    public function Jpush($token, $msg, $type, $msg_data)
    {
        $ji = array(
            'appKey' => '8614b01a9733364444c8c7d4',
            'masterSecret' => '35a4e075f5d1ddcb4b3c25f3',
        );
        $client = new \JPush\Client($ji['appKey'], $ji['masterSecret']);
        $push = $client->push();
        $push->setPlatform('all');
        if($type == 1) {
            $push->addAlias($token);
        } else {
            $push->addAllAudience();
        }
        $push->iosNotification($msg, array(
                'alert' => $msg,
                'badge' => '1',
                'extras' => $msg_data,
                'sound' => 'default'
            )
        );
        $push->androidNotification($msg, array(
            'badge' => '1',
            'extras' => $msg_data
        ));
        return $push->send();
        
    }
}